from django.shortcuts import render
from django.views import View
import logging

from common.models import TbMiniappUser
from src.utils.record import director
from src.utils.ReturnStatus import ResponseStatus


# Create your views here.
class leaderBoardView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取排行榜
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.LeaderboardDirector().get_leaderboard(**query_params)
        return ResponseStatus().success(data=result)


class self_leaderBoardView(View):
    def get(self, request, user: TbMiniappUser):
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.LeaderboardDirector().get_self_leaderboard(**query_params)
        return ResponseStatus().success(data=result)


class get_record_detail(View):
    def get(self, request, user: TbMiniappUser):
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.LeaderboardDirector().get_record_detail(**query_params)
        return ResponseStatus().success(data=result)


class LikeView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取最近一次点赞记录
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.LeaderboardDirector().get_last_like_record(**query_params)
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        点赞/取消点赞
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.LeaderboardDirector().like(**post_data)
        return ResponseStatus().success(data=result, message="点赞成功")


class GameView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取赛事列表
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        count, result = director.GameDirector().get_game_list(**query_params)
        result = {
            "count": count,
            "data": result
        }
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        发布赛事
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.GameDirector().publish_game(**post_data)
        return ResponseStatus().success(data=result)


class GameDetailView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取赛事详情
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.GameDirector().get_game_detail(**query_params)
        return ResponseStatus().success(data=result)


class GamePicView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取赛事的赛事图
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.GameDirector().get_game_list(**query_params)
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        给赛事添加赛事图
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.GameDirector().publish_game(**post_data)
        return ResponseStatus().success(data=result)


class GameApplyView(View):
    """
    赛事报名
    """

    def post(self, request, user: TbMiniappUser):
        """
        赛事报名
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.GameDirector().apply(**post_data)
        return ResponseStatus().success(data=result, message="报名成功")


class ApplyPriceView(View):
    def get(self, request, user: TbMiniappUser):
        """
        查询支付结果
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.ApplyDirector().get_pay_result(**query_params)
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        支付报名费用
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.ApplyDirector().pay_apply_price(**post_data)
        return ResponseStatus().success(data=result, message="开始支付")


class ApplyPaySuccess(View):
    def post(self, request, user: TbMiniappUser):
        """
        支付报名费用成功
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.ApplyDirector().apply_pay_success(**post_data)
        return ResponseStatus().success(data=result, message="报名成功")


def setlist(request):
    """
    未登录获取场地列表
    """
    query_params = request.GET.dict()
    count, result = director.SiteDirector().get_site(**query_params)
    result = {
        "count": count,
        "data": result
    }
    return ResponseStatus().success(data=result)


class SiteView(View):
    """
    场地
    """

    def get(self, request, user: TbMiniappUser):
        """
        获取场地列表
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        count, result = director.SiteDirector().get_site(**query_params)
        result = {
            "count": count,
            "data": result
        }
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        添加场地
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().add_site(**post_data)
        return ResponseStatus().success(data=result, message="添加成功")


class SiteDetailView(View):
    """
    场地详情
    """

    # def get(self, request, user: TbMiniappUser):
    def get(self, request):
        """
        获取场地详情
        """
        query_params = request.GET.dict()
        # query_params["user"] = user
        result = director.SiteDirector().get_site_detail(**query_params)
        return ResponseStatus().success(data=result)


class SiteApplyStatusView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取是否重复报名
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.SiteDirector().get_apply_status(**query_params)
        return ResponseStatus().success(data=result)


class SiteApplyView(View):
    """
    场地报名
    """

    def get(self, request, user: TbMiniappUser):
        """
        获取场地报名情况
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        result = director.SiteDirector().get_apply_info(**query_params)
        return ResponseStatus().success(data=result)

    def post(self, request, user: TbMiniappUser):
        """
        场地报名
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().site_apply(**post_data)
        return ResponseStatus().success(data=result, message="报名成功")


class SiteApplyPayView(View):
    """
    场地报名支付
    """

    def post(self, request, user: TbMiniappUser):
        """
        场地报名支付
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().pay_apply_price(**post_data)
        return ResponseStatus().success(data=result, message="报名成功")


class SiteApplyPaySuccessView(View):
    def post(self, request, user: TbMiniappUser):
        """
        报名成功
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().apply_pay_success(**post_data)
        return ResponseStatus().success(data=result, message="报名成功")


class SiteApplyRefundView(View):
    def post(self, request, user: TbMiniappUser):
        """
        退款
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().refund(**post_data)
        return ResponseStatus().success(data=result, message="退款成功")


class MySiteApplyView(View):
    def get(self, request, user: TbMiniappUser):
        """
        获取我的场地申请
        """
        query_params = request.GET.dict()
        query_params["user"] = user
        count, result = director.SiteDirector().get_my_apply_list(**query_params)
        result = {
            "count": count,
            "data": result
        }
        return ResponseStatus().success(data=result)


class OcrView(View):
    def post(self, request, user: TbMiniappUser):
        """
        图像识别
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().ocr(**post_data)
        return ResponseStatus().success(data=result, message="识别成功")


class FansView(View):
    def post(self, request, user: TbMiniappUser):
        """
        关注
        """
        post_data = eval(request.body.decode())
        post_data["user"] = user
        result = director.SiteDirector().ocr(**post_data)
        return ResponseStatus().success(data=result, message="识别成功")
